var roleHarout = {
    run: function(creep) {
        //creep.say(creep.memory.building);

        if(creep.store.getFreeCapacity()>0){
            creep.memory.building = true;
        }
        else{
            creep.memory.building = false;
        }


        if(creep.memory.building == true){
            if(creep.room.name != 'E9S57'){
                //creep.say(321);
                creep.moveTo(Game.flags['Flag9'].pos);
            }
            else{
                //creep.say(321321);
                var sources = creep.room.find(FIND_SOURCES);
                if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(sources[1], {visualizePathStyle: {stroke: '#ffaa00'}});
                }
            }
        }
        else{
            if(creep.room.name != 'E9S58'){
                //creep.say(123);
                //修路
                const targetsroad = creep.room.find(FIND_STRUCTURES, {
                    filter: (structure) => structure.hits < structure.hitsMax*0.5 && structure.structureType == STRUCTURE_ROAD
                });
                targetsroad.sort((a,b) => a.hits - b.hits);
                if(targetsroad.length > 0) {
                    if(creep.repair(targetsroad[0]) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(targetsroad[0],{visualizePathStyle: {stroke: '#ffaa00'}});
                        //creep.say('修去');
                    }
                }
                else{
                    //回家
                    creep.moveTo(Game.flags['Flag10'].pos);
                }
                
            }
            else{
                //放能量
                var targets = creep.room.find(FIND_STRUCTURES, {
                    filter: (structure) => {
                        return (structure.structureType == STRUCTURE_STORAGE) && 
                        structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
                    }
                });
                if(targets.length > 0) {
                    if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(targets[0]);
                    }
                }
            }
        }
	}
};

module.exports = roleHarout;